﻿using System;
using System.Collections.Generic;
using System.Text;
using OgilvyOne.IDAL;
using OgilvyOne.Model;
using System.Data;
using System.Data.SqlClient;

namespace OgilvyOne.SqlServerDAL
{
    public class LogManage : LogManageIDAL
    {
        string connstr = SQLHelper.CONN_STRING_NON_DTC;
        //读取日志
        public System.Data.DataSet GetLog(string strLog)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select l.*,u.USERNAME from CMS_LOGS l left join cms_users u on l.MEMBERID=u.aid ");
            if (strLog.Trim() != "")
            {
                strSql.Append(" where " + strLog);
            }
            strSql.Append(" order by EXECUTETIME desc");
            return SQLHelper.Query(strSql.ToString());
        }
        //插入日志
        public void insert(Model.LogManage strLog)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into CMS_LOGS (MEMBERID,LOGCONTENT,EXECUTETIME)");
            strSql.Append(" values (@MEMBERID,@LOGCONTENT,@EXECUTETIME)");
            SqlParameter[] parameters = {
                    new SqlParameter("@MEMBERID",SqlDbType.VarChar,50),
                    new SqlParameter("@LOGCONTENT",SqlDbType.VarChar,255),
                    new SqlParameter("@EXECUTETIME",SqlDbType.DateTime),
                                        };
            parameters[0].Value = strLog.Memberid;
            parameters[1].Value = strLog.Logcontent;
            parameters[2].Value = strLog.Executetime;
            int ts = 1;
            ts = SQLHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        //删除日志
        public void Del(string starttime, string endtime)
        {
            string delsql = "delete from CMS_LOGS where EXECUTETIME>='" + starttime + "' and EXECUTETIME<='" + endtime + "'";
            SqlParameter[] parameters = {
                   
                                        };
            int ts = 1;
            ts = SQLHelper.ExecuteSql(delsql, parameters);
        }

        //日志开关设置
        public void SwitchsLogs(LogSwitchs ls)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update CMS_LOGS_SWITCHS set switchs=@switchs where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@id",SqlDbType.Int),
                    new SqlParameter("@switchs",SqlDbType.Int)
                                        };
            parameters[0].Value = ls.Id;
            parameters[1].Value = ls.Switchs;

            int ts = 1;
            ts = SQLHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        //日志开关查询
        public LogSwitchs selectLogsSwitch()
        {
            LogSwitchs ls = new LogSwitchs();
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select switchs from CMS_LOGS_SWITCHS where id=1");
                SqlParameter[] parms = {
                                        };
                using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, strSql.ToString(), parms))
                {
                    if (rdr.Read())
                    {
                        ls.Id = rdr.GetInt32(0);
                        ls.Switchs = rdr.GetInt32(0);
                    }
                }
            }
            catch(Exception ex)
            { 
            }
            return ls;
        }
    }
}
